package com.koron.rating.rating;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.FastJsonUtils;
import com.koron.common.core.util.StringUtils;
import com.koron.rating.rating.bean.RatingOrderVo;
import com.koron.rating.units.RatingUnits;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: rocky
 * @Date: 2022/1/11 9:46
 */
@Slf4j
@RequestMapping("/rating/workflow")
@RestController
public class RatingWorkflowController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @Autowired
    private RatingService ratingservice;

    @Autowired
    private SystemFeignService systemfeignservice;//获取审批人 公共接口

//    @PostMapping("/callstart/handler")


    @PostMapping("/draftwrite/submitter")
    public Response<List<WorkflowUser>> draftwrite(@RequestBody SubmitterParam submitterParam) {
        //选择 各单位评级负责人会审
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 1);
        return Response.success(list);
    }

    @PostMapping("/callstart/submitter")
    public Response<List<WorkflowUser>> callacceptOper(@RequestBody SubmitterParam submitterParam) {
        //选择 部门分管领导审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 2);
        return Response.success(list);
    }

    @PostMapping("/leader/submitter")
    public Response<List<WorkflowUser>> leader(@RequestBody SubmitterParam submitterParam) {
        //选择 部门负责人审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 3);
        return Response.success(list);
    }

    @PostMapping("/deptleader/submitter")
    public Response<List<WorkflowUser>> deptleader(@RequestBody SubmitterParam submitterParam) {
        //生产技术部专业管理审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 4);
        return Response.success(list);
    }

    @PostMapping("/major/submitter")
    public Response<List<WorkflowUser>> major(@RequestBody SubmitterParam submitterParam) {
        //选择 生产技术部负责人审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 5);
        return Response.success(list);
    }

    @PostMapping("/majorleader/submitter")
    public Response<List<WorkflowUser>> majorleader(@RequestBody SubmitterParam submitterParam) {
        //选择 公司分管领导审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 6);//公司分管领导（机电）（岗位
        return Response.success(list);
    }


    /**
     * 获取下一机电审批人
     *
     * @param submitterParam
     * @param type
     * @return
     */
    public List<WorkflowUser> getWorkflowUsers(SubmitterParam submitterParam, Integer type) {
        List<WorkflowUser> list = new ArrayList<>();
        RatingOrderVo com = FastJsonUtils.getJsonToBean(submitterParam.getFormVariables(), RatingOrderVo.class);
        log.info("接收到的参数:" + FastJsonUtils.getBeanToJson(submitterParam));

        //选择 各单位评级负责人会审
        if (type == 1) {
            list = com.getDet().stream().map(co -> {
                WorkflowUser appuser = new WorkflowUser();
                appuser.setCode(co.getConfereeId());
                appuser.setName(co.getConfereeName());
                return appuser;
            }).collect(Collectors.toList());
        }

        //2 选择 部门分管领导审批
        if (type == 2) {
            //获取配置的岗位id
            List<String> postid = RatingUnits.getPostId(submitterParam);//岗位在业务配置设置
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);
            postUserQuery.setParentOrgIdList(CollUtil.newArrayList(com.getDeptId()));
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
//            List<PostUserVo> temlist = pulist.stream().filter(pu -> pu.getDeptId().equals(com.getDeptId())).collect(Collectors.toList());
            list = reduceUser(pulist);
        }

        //3 选择 部门负责人审批
        if (type == 3) {
            //获取配置的岗位id
            List<String> postid = RatingUnits.getPostId(submitterParam);//岗位在业务配置设置
            String deptId = RatingUnits.queryDeptByOrgId(com.getDeptId());//获取-创建人所在部门的部门
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);//部门总经理（岗位）
            postUserQuery.setParentOrgIdList(CollUtil.newArrayList(deptId));//只发送给创建人所在部门的部门总经理
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
            list = reduceUser(pulist);
        }

        //选择 4 生产技术部专业管理审批
        //选择 5 生产技术部负责人审批
        //选择 6 公司分管领导审批
        if (type == 4 || type == 5 || type == 6) {
            List<String> postid = RatingUnits.getPostId(submitterParam);//指定岗位在业务配置设置
            List<String> deptId = RatingUnits.getDeptId(submitterParam);//指定部门在业务配置设置
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);
            if (StringUtils.isNotEmpty(deptId)) {
                postUserQuery.setParentOrgIdList(deptId);// 5只发送至生产技术部的部门总经理
            }
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
            list = reduceUser(pulist);
        }

        log.info("返回人员:" + FastJsonUtils.getBeanToJson(list));
        return list;
    }


    /**
     * 调公共接口查审批人员
     * 查出的人员 都有岗位的
     *
     * @param postUserQuery
     * @return
     */
    public List<PostUserVo> getWorkflowUsers(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> listResponse = systemfeignservice.queryPostList(postUserQuery);//查出符合条件的审批人
        Assert.isFalse(listResponse.getCode() != 200, listResponse.getDescription());
        if (listResponse.getCode() == 200) {
            List<PostUserVo> ulist = listResponse.getData();
            if (StringUtils.isEmpty(ulist)) {
                return new ArrayList();
            }
            return ulist;
        }
        return null;
    }

    /**
     * 归集人员
     *
     * @param ulist
     */
    private List<WorkflowUser> reduceUser(List<PostUserVo> ulist) {
        if (StringUtils.isEmpty(ulist)) {
            return new ArrayList<>();
        }
        return ulist.stream().map(vo -> {
            WorkflowUser workflowuser = new WorkflowUser();
            workflowuser.setCode(vo.getUserAccount());
            workflowuser.setName(vo.getUserName());
            return workflowuser;
        }).collect(Collectors.toList());
    }

}
